package mulegame;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Regulates the timer, sets it up and determines when it stops ticking. Makes action listeners for the timer
 * 
 * @author AREA 29
 *
 */
public class TimedScreen extends JPanel{
    protected GraphicsControl control;
    private Timer ticks;
    private Timer timeOut;
    protected int duration;


   /**
    * sets up the timer and when it should repeat and then makes the panel
    * 
    * @param ctrl graphics controller object 
    * @param duration duration of turn
    */
    public TimedScreen(final GraphicsControl ctrl, final int duration){
        super();
        control = ctrl;
        this.duration = duration;
        ticks = new Timer(1000, new tick());    //1000ms = 1 sec 
                                                //basically tick every
                                                //second
        timeOut = new Timer(duration, new timeOut());
        timeOut.setRepeats(false);  //this is kinda not necessary since the timers are stopped when this timer's event happens
        
        //start the timers
        ticks.start();
        timeOut.start();
    }  
    
   /**
    * logger print out that the timer ticked
    */
    //override this method to update Panel every second
    public void timerTick(){
        Logger.println("timer ticked");   
    }
    
    /**
     * logger print out that the timer timed out
     */
    //override this method to handle the time running out 
    public void timerTimeOut(){
        Logger.println("timer timed out");
    }
    
    /**
     * stops the tick and stop the timer
     */
    protected void stopTimers(){
        ticks.stop();
        timeOut.stop();
    }
    
    /**
     * creates an action listener for the timer to tick
     * 
     * @author AREA 29
     *
     */
    private class tick implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent evnt){
            timerTick();
        }
    }
    
    /**
     * creates an action listener for timer to stop ticking when it has timed out
     * 
     * @author AREA 29
     *
     */
    private class timeOut implements ActionListener{
        public void actionPerformed(ActionEvent evnt){
            timerTimeOut();
            stopTimers();
        }
    }
}
